﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace FrameWork.WinUI.Common
{
    public static class ListViewBaseExt
    {
        public static FrameWork.Lib.DataObjects.DataObjectBase SelectedDataObjectByGuid(this System.Windows.Forms.ListView pListView, List<FrameWork.Lib.DataObjects.DataObjectBase> pDataObjectList)
        {
            if (pListView.SelectedItems.Count > 0)
            {
                var tGUID = ((Guid)pListView.SelectedItems[0].Tag);

                var tDataObject = pDataObjectList.Where(t => t.DataObjectGUID == tGUID).FirstOrDefault();

                return tDataObject;
            }

            return null;
        }

        public static T SelectedGroupDataObject<T>(this System.Windows.Forms.ListView pListView)
        {

            if (pListView.SelectedItems.Count > 0)
            {
                var t = ((T)pListView.SelectedItems[0].Group.Tag);
                return t;
            }
            return default(T);
        }

        public static T SelectedDataObject<T>(this System.Windows.Forms.ListView pListView)
        {
            if (pListView.SelectedItems.Count > 0)
            {
                var t = ((T)pListView.SelectedItems[0].Tag);
                return t;
            }
            return default(T);
        }

        public static void AutoResizeHeaderSize(this System.Windows.Forms.ColumnHeader pColumnHeader)
        {
            pColumnHeader.AutoResize(System.Windows.Forms.ColumnHeaderAutoResizeStyle.HeaderSize);
        }

        public static void AutoResizeColumnContent(this System.Windows.Forms.ColumnHeader pColumnHeader)
        {
            pColumnHeader.AutoResize(System.Windows.Forms.ColumnHeaderAutoResizeStyle.ColumnContent);
        }

        public static void AutoResize(this System.Windows.Forms.ColumnHeader pColumnHeader)
        {

            pColumnHeader.AutoResize(System.Windows.Forms.ColumnHeaderAutoResizeStyle.ColumnContent);

            if (pColumnHeader.Width == 25)
            {
                pColumnHeader.AutoResize(System.Windows.Forms.ColumnHeaderAutoResizeStyle.HeaderSize);
            }
        }

        public static void AutoResize(this System.Windows.Forms.ListView pListView)
        {
            if (pListView.Items.Count > 0)
            {
                foreach (System.Windows.Forms.ColumnHeader t in pListView.Columns)
                {
                    t.AutoResize();
                }
            }
        }
    }
}
